<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" >
    <meta http-equiv="X-UA-Compatible" content="IE=11,IE=10,IE=9,IE=8" >
    <meta name="baidu-site-verification" content="dIcXMeY8Ya" />
    
    <title>Hibernate-Entity.hbm.xml实现-intellij-kotlin入门级 | 即将拥有人鱼线丶</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" >
    <meta name="keywords" content="FutaoSmile, 前端, Web, 付韬, 个人博客" >
    <meta name="description" content="付韬FutaoSmile" >

    
    <link rel="alternative" href="/atom.xml" title="即将拥有人鱼线丶" type="application/atom+xml" >
    
    
    <link rel="shortcut icon" href="/coder957/coder957/favicon.ico" >
    
    <link rel="stylesheet" href="/coder957/css/style.css">
    <!--[if lt IE 9]>
    <script src="/coder957/js/html5.js"></script>
    <![endif]-->
    
<script>
    var _hmt = _hmt || [];
    (function() {
        var hm = document.createElement("script");
        hm.src = "//hm.baidu.com/hm.js?fd459238242776d173cdc64918fb32f2";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>


</head>

<body class="home">
    <!--[if lt IE 9]>
    <div class="browsehappy">
        当前网页 <strong>不支持</strong>
        你正在使用的浏览器. 为了正常的访问, 请 <a href="http://browsehappy.com/">升级你的浏览器</a>.
    </div>
    <![endif]-->

    <!-- 博客头部 -->
    <header class="header">
    <section class="container header-main">
        <div class="logo">
            <a href="/coder957/">
                <div class="cover">
                    <span class="name">即将拥有人鱼线丶</span>
                    <span class="description">FutaoSmile</span>
                </div>
            </a>
        </div>
        <div class="dropnav icon-paragraph-justify" id="JELON__btnDropNav"></div>
        <ul class="menu hidden" id="JELON__menu">
            
                <li rel="/coder957/2017/09/26/Hibernate-Entity-hbm-xml实现-intellij-kotlin入门级/index.html" class="item ">
                    <a href="/coder957/" title="首页" class="icon-home">&nbsp;首页</a>
                </li>
                
                <li rel="/coder957/2017/09/26/Hibernate-Entity-hbm-xml实现-intellij-kotlin入门级/index.html" class="item ">
                    <a href="/coder957/lab/" title="实验室" class="icon-lab">&nbsp;实验室</a>
                </li>
                
                <li rel="/coder957/2017/09/26/Hibernate-Entity-hbm-xml实现-intellij-kotlin入门级/index.html" class="item ">
                    <a href="/coder957/about/" title="关于" class="icon-about">&nbsp;关于</a>
                </li>
                
                <li rel="/coder957/2017/09/26/Hibernate-Entity-hbm-xml实现-intellij-kotlin入门级/index.html" class="item ">
                    <a href="/coder957/comment/" title="留言" class="icon-comment">&nbsp;留言</a>
                </li>
                
        </ul>
        <div class="profile clearfix">
            <div class="feeds fl">
                
                    
                        <p class="links">
                            
                                <a href="https://github.com/FutaoSmile" target="_blank">
                                    Github
                                </a>
                                |
                                    
                                        
                                <a href="http://www.jianshu.com/u/9e2e579df7dd" target="_blank">
                                    简书
                                </a>
                                
                                        
                        </p>
                        <p class="sns">
                            
                                <a href="http://weibo.com/u/2379076265?refer_flag=1001030001_&amp;nick=FutaoSmile%E4%B8%B6" class="sinaweibo" target="_blank"><b>■</b> 新浪微博</a>
                                
                                <a href="https://gitee.com/FutaoSmile" class="qqweibo" target="_blank"><b>■</b> 码云</a>
                                
                                    <!-- <a href="javascript: void(0);" class="wechat">
                        <b>■</b>
                        公众号
                        <span class="popover">
                            <img src="/coder957/img/wechat_mp.jpg" width="120" height="120" alt="我的微信订阅号">
                            <i class="arrow"></i>
                        </span>
                    </a> -->
                        </p>
                        
            </div>
            <div class="avatar fr">
                <img src="/coder957/img/head.png" alt="avatar" title="Jelon">
            </div>
        </div>
    </section>
</header>

    <!-- 博客正文 -->
    <div class="container body clearfix">
        <section class="content">
            <div class="content-main widget">
                <!-- 文章页 -->
<!-- 文章 -->
<article class="post article">
    <header class="text-center">
        <h3 class="post-title"><span>Hibernate-Entity.hbm.xml实现-intellij-kotlin入门级</span></h3>
    </header>
    <p class="post-meta text-center">
        FutaoSmile丶 发表于
            <time datetime="2017-09-26T08:25:04.350Z">2017-09-26</time>
    </p>
    <div class="post-content">
        <blockquote>
<p><a href="https://gitee.com/FutaoSmile/hibernate-imooc-forBeginner" target="_blank" rel="external">项目地址-码云</a></p>
</blockquote>
<ol>
<li>intellij新建Maven项目</li>
<li>在src/resource下新建hibernate.cfg.xml文件，一定要在src/resource目录下，否则会报无法定位资源文件的错误。</li>
</ol>
<a id="more"></a>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;</div><div class="line">&lt;!DOCTYPE hibernate-configuration PUBLIC</div><div class="line">        &quot;-//Hibernate/Hibernate Configuration DTD//EN&quot;</div><div class="line">        &quot;http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd&quot;&gt;</div><div class="line">&lt;hibernate-configuration&gt;</div><div class="line">    &lt;session-factory&gt;</div><div class="line">        &lt;!--数据库url，以及防止中文乱码--&gt;</div><div class="line">        &lt;property name=&quot;connection.url&quot;&gt;jdbc:mysql://localhost:3306/hibernate-final?useUnicode=true&amp;amp;characterEncoding=UTF-8&lt;/property&gt;</div><div class="line">        &lt;!--Mysql驱动--&gt;</div><div class="line">        &lt;property name=&quot;connection.driver_class&quot;&gt;com.mysql.jdbc.Driver&lt;/property&gt;</div><div class="line">        &lt;!--Mysql用户名--&gt;</div><div class="line">        &lt;property name=&quot;connection.username&quot;&gt;root&lt;/property&gt;</div><div class="line">        &lt;!--Mysql密码--&gt;</div><div class="line">        &lt;property name=&quot;connection.password&quot;&gt;rootPwd&lt;/property&gt;</div><div class="line"></div><div class="line">        &lt;!--方言--&gt;</div><div class="line">        &lt;property name=&quot;dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;</div><div class="line">        &lt;!--调试时显示sql语句--&gt;</div><div class="line">        &lt;property name=&quot;show_sql&quot;&gt;true&lt;/property&gt;</div><div class="line">        &lt;!--格式化调试时输出的sql语句--&gt;</div><div class="line">        &lt;property name=&quot;format_sql&quot;&gt;true&lt;/property&gt;</div><div class="line"></div><div class="line">        &lt;!-- DB schema will be updated if needed --&gt;</div><div class="line">        &lt;!--create表结构每次发生改变都会删除原来的表，创建新的表。update是在原有的表上进行更新，保留数据--&gt;</div><div class="line">        &lt;property name=&quot;hbm2ddl.auto&quot;&gt;update&lt;/property&gt;</div><div class="line">        &lt;!--&lt;property name=&quot;hbm2ddl.auto&quot;&gt;create&lt;/property&gt;--&gt;</div><div class="line"></div><div class="line">        &lt;!--映射文件--&gt;</div><div class="line">        &lt;mapping resource=&quot;StudentEntity.hbm.xml&quot;/&gt;</div><div class="line">    &lt;/session-factory&gt;</div><div class="line">&lt;/hibernate-configuration&gt;</div></pre></td></tr></table></figure>
<ol>
<li>不要使用intellij的Add Framework Support，可能是intellij的hibernate版本太低了，使用这种方式生成数据库表的时候会报错，”type”在mysql高版本中已经被删除。</li>
<li><p>在项目的pom文件中添加hibernate依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">&lt;!--hibernate依赖--&gt;</div><div class="line">  &lt;dependency&gt;</div><div class="line">      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;</div><div class="line">      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;</div><div class="line">      &lt;version&gt;4.3.11.Final&lt;/version&gt;</div><div class="line">  &lt;/dependency&gt;</div><div class="line">  &lt;!--单元测试依赖--&gt;</div><div class="line">  &lt;dependency&gt;</div><div class="line">      &lt;groupId&gt;junit&lt;/groupId&gt;</div><div class="line">      &lt;artifactId&gt;junit&lt;/artifactId&gt;</div><div class="line">      &lt;version&gt;4.10&lt;/version&gt;</div><div class="line">  &lt;/dependency&gt;</div><div class="line">  &lt;!--mysqlJDBC依赖--&gt;</div><div class="line">  &lt;dependency&gt;</div><div class="line">      &lt;groupId&gt;mysql&lt;/groupId&gt;</div><div class="line">      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;</div><div class="line">      &lt;version&gt;5.1.39&lt;/version&gt;</div><div class="line">  &lt;/dependency&gt;</div></pre></td></tr></table></figure>
</li>
<li><p>新建持久化类</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div></pre></td><td class="code"><pre><div class="line">package Entity;</div><div class="line"></div><div class="line"></div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/24.</div><div class="line"> */</div><div class="line">public class Student &#123;</div><div class="line">    //JavaBean(持久化类)编写规范</div><div class="line">    //1.公有的类</div><div class="line">    //2.私有属性</div><div class="line">    //3.提供公有的不带参数的默认构造方法</div><div class="line">    //4.私有属性的公有getter和setter</div><div class="line">    //5.为方便实例化对象，一般生成一个带参的构造方法</div><div class="line"></div><div class="line">    //设置public static final String的意义在于：比如 .add(Restrictions.eq())的时候，可以直接通过实体的类型点出来，而不用手动输入，这样避免了手动输入发生错误的情况</div><div class="line">    private int sid;</div><div class="line">    public static final String _sid = &quot;sid&quot;;</div><div class="line">    private String name;</div><div class="line">    public static final String _name = &quot;name&quot;;</div><div class="line">    private String gender;</div><div class="line">    public static final String _gender = &quot;gender&quot;;</div><div class="line">    private Timestamp birthday;</div><div class="line">    public static final String _birthday = &quot;birthday&quot;;</div><div class="line">    private String address;</div><div class="line">    public static final String _address = &quot;address&quot;;</div><div class="line">    private String tel;</div><div class="line">    public static final String _tel = &quot;tel&quot;;</div><div class="line"></div><div class="line">    public Student()&#123; &#125;</div><div class="line"></div><div class="line">    public Student(int sid, String name, String gender, String birthday, String address, String tel) &#123;</div><div class="line">        this.sid = sid;</div><div class="line">        this.name = name;</div><div class="line">        this.gender = gender;</div><div class="line">        this.birthday = birthday;</div><div class="line">        this.address = address;</div><div class="line">        this.tel = tel;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public int getSid() &#123;</div><div class="line">        return sid;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void setSid(int sid) &#123;</div><div class="line">        this.sid = sid;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public String getName() &#123;</div><div class="line">        return name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void setName(String name) &#123;</div><div class="line">        this.name = name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public String getGender() &#123;</div><div class="line">        return gender;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void setGender(String gender) &#123;</div><div class="line">        this.gender = gender;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public String getBirthday() &#123;</div><div class="line">        return birthday;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void setBirthday(String birthday) &#123;</div><div class="line">        this.birthday = birthday;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public String getAddress() &#123;</div><div class="line">        return address;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void setAddress(String address) &#123;</div><div class="line">        this.address = address;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public String getTel() &#123;</div><div class="line">        return tel;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void setTel(String tel) &#123;</div><div class="line">        this.tel = tel;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
<li><p>在src/resource文件夹下创建实体映射文件<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">```</div><div class="line">&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;</div><div class="line">&lt;!DOCTYPE hibernate-mapping PUBLIC</div><div class="line">    &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;</div><div class="line">    &quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;</div><div class="line">&lt;hibernate-mapping&gt;</div><div class="line"></div><div class="line">    &lt;class name=&quot;Entity.Student&quot; table=&quot;t_student&quot;&gt;</div><div class="line">        &lt;id name=&quot;sid&quot;&gt;</div><div class="line">            &lt;column name=&quot;sid&quot; sql-type=&quot;int(11)&quot;/&gt;</div><div class="line">            &lt;generator class=&quot;native&quot;/&gt;</div><div class="line">        &lt;/id&gt;</div><div class="line">        &lt;property name=&quot;name&quot;&gt;</div><div class="line">            &lt;column name=&quot;name&quot; sql-type=&quot;varchar(255)&quot; not-null=&quot;true&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">        &lt;property name=&quot;birthday&quot;&gt;</div><div class="line">            &lt;column name=&quot;birthday&quot; sql-type=&quot;varchar(255)&quot; not-null=&quot;true&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">        &lt;property name=&quot;address&quot;&gt;</div><div class="line">            &lt;column name=&quot;address&quot; sql-type=&quot;varchar(255)&quot; not-null=&quot;false&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">    &lt;/class&gt;</div><div class="line">&lt;/hibernate-mapping&gt;</div></pre></td></tr></table></figure></p>
</li>
<li><p>在hibernate主配置文件中添加实体映射文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;!--映射文件--&gt;</div><div class="line">      &lt;mapping resource=&quot;StudentEntity.hbm.xml&quot;/&gt;</div></pre></td></tr></table></figure>
</li>
<li><p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">package Entity</div><div class="line"></div><div class="line">import org.hibernate.cfg.Configuration</div><div class="line">import org.junit.Test</div><div class="line"></div><div class="line">@Suppress(&quot;DEPRECATION&quot;)</div><div class="line">/**</div><div class="line"> * Created by futao on 2017/9/24.</div><div class="line"> */</div><div class="line">class t&#123;</div><div class="line"></div><div class="line">    @Test</div><div class="line">    fun testInit()&#123;</div><div class="line">        //加载配置文件</div><div class="line">        val configuration=Configuration().configure()</div><div class="line">        //创建会话工厂</div><div class="line">        val sessionFactory=configuration.buildSessionFactory()</div><div class="line">        //创建会话</div><div class="line">        val session=sessionFactory.openSession()</div><div class="line">        //开启事务</div><div class="line">        val transaction=session.beginTransaction()</div><div class="line"></div><div class="line">        //crud</div><div class="line">        session.save(Student(1,&quot;熊小二&quot;,&quot;男&quot;, &quot;2017-9-24&quot;,&quot;上海市小二房&quot;,&quot;1887978252&quot;))</div><div class="line"></div><div class="line">        //提交事务</div><div class="line">        transaction.commit()</div><div class="line">        //关闭会话</div><div class="line">        session.close()</div><div class="line">        //关闭会话工厂</div><div class="line">        sessionFactory.close()</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="——–"><a href="#——–" class="headerlink" title="——–"></a>——–</h2><h3 id="基础版完成-↑↑↑↑↑↑↑"><a href="#基础版完成-↑↑↑↑↑↑↑" class="headerlink" title="基础版完成 ↑↑↑↑↑↑↑"></a>基础版完成 ↑↑↑↑↑↑↑</h3><h2 id="——–-1"><a href="#——–-1" class="headerlink" title="——–"></a>——–</h2><h4 id="Hibernate执行流程"><a href="#Hibernate执行流程" class="headerlink" title="Hibernate执行流程"></a>Hibernate执行流程</h4><p><img src="http://upload-images.jianshu.io/upload_images/1846623-4d1e3c516fc1debf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="Hibernate执行流程"></p>
<p>session简单地理解就是一个操作数据库的对象<br>hibernate的操作必须包装在事务当中，否则操作不会同步到数据库（或者使用doWord()的方式）</p>
<h6 id="创建session对象的两种方式"><a href="#创建session对象的两种方式" class="headerlink" title="创建session对象的两种方式"></a>创建session对象的两种方式</h6><ol>
<li>val session=sessionFactory.openSession()</li>
<li>val session=sessionFactory.currentSession<br>使用第二种方式获得session需要在hibernate主配置文档中进行配置<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;!-- Enable Hibernate&apos;s automatic session context management --&gt;</div><div class="line">       &lt;property name=&quot;current_session_context_class&quot;&gt;thread&lt;/property&gt;</div></pre></td></tr></table></figure>
</li>
</ol>
<h5 id="hbm常用配置"><a href="#hbm常用配置" class="headerlink" title="hbm常用配置"></a>hbm常用配置</h5><p><img src="http://upload-images.jianshu.io/upload_images/1846623-0498f928fb770bbe.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="mapping"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-9102080d8dc3ec6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="class"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-03a0d2d4d8c9cccc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="id"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-88f39e7b53350ee3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="主键生成策略"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-0187aa3e247235b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="基本类型"></p>
<p>timestamp时间戳</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-437d6ec7a5a02896.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-4f6220e5487b32d4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="长文本"></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">//Entity</div><div class="line">  private Blob picture;//照片,长文本</div><div class="line">    public static final String _picture = &quot;picture&quot;;</div><div class="line"></div><div class="line"></div><div class="line">//hbm.xml</div><div class="line">       &lt;property name=&quot;picture&quot;&gt;</div><div class="line">            &lt;column name=&quot;picture&quot; sql-type=&quot;longblob&quot; not-null=&quot;false&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line"></div><div class="line"></div><div class="line">//Test    存</div><div class="line"> val f= File(&quot;d:&quot;+File.separator+&quot;banner.png&quot;)</div><div class="line">        val input=FileInputStream(f)</div><div class="line">        val blob=Hibernate.getLobCreator(session).createBlob(input, input.available().toLong())</div><div class="line">        session.save(Student(5,&quot;熊&quot;,&quot;男&quot;, Timestamp(DateTime.now().millis) ,&quot;上海市小二房&quot;,&quot;1887978252&quot;,blob))</div><div class="line"></div><div class="line"></div><div class="line">//    取</div><div class="line"> val entity = session.get(Student::class.java, 4) as Student</div><div class="line">        //获得Blob对象</div><div class="line">        val pic= entity.picture</div><div class="line">        //获得照片的输入流</div><div class="line">        val inPut=pic.getBinaryStream()</div><div class="line">        //创建输出流</div><div class="line">        val file = File(&quot;d:&quot;+File.separator+&quot;bannerNew.png&quot;)</div><div class="line">        //获得输出流</div><div class="line">        val outPut=FileOutputStream(file)</div><div class="line">        //创建缓冲区</div><div class="line">        var buff = ByteArray(inPut.available())</div><div class="line">        inPut.read(buff)</div><div class="line">        outPut.write(buff)</div><div class="line">        inPut.close()</div><div class="line">        outPut.close()</div></pre></td></tr></table></figure>
<p>组件属性</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-a8362c07c608fb99.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="组件属性"><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;</div><div class="line">&lt;!DOCTYPE hibernate-mapping PUBLIC</div><div class="line">        &quot;-//Hibernate/Hibernate Mapping DTD 3.0//EN&quot;</div><div class="line">        &quot;http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd&quot;&gt;</div><div class="line">&lt;hibernate-mapping&gt;</div><div class="line"></div><div class="line">    &lt;class name=&quot;Entity.Student&quot; table=&quot;t_student&quot;&gt;</div><div class="line">        &lt;id name=&quot;sid&quot;&gt;</div><div class="line">            &lt;column name=&quot;sid&quot; sql-type=&quot;int(11)&quot;/&gt;</div><div class="line">            &lt;generator class=&quot;native&quot;/&gt;</div><div class="line">        &lt;/id&gt;</div><div class="line">        &lt;property name=&quot;name&quot;&gt;</div><div class="line">            &lt;column name=&quot;name&quot; sql-type=&quot;varchar(255)&quot; not-null=&quot;true&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">        &lt;property name=&quot;birthday&quot;&gt;</div><div class="line">            &lt;column name=&quot;birthday&quot; sql-type=&quot;timestamp&quot; not-null=&quot;true&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">        &lt;!--&lt;property name=&quot;address&quot;&gt;--&gt;</div><div class="line">            &lt;!--&lt;column name=&quot;address&quot; sql-type=&quot;varchar(255)&quot; not-null=&quot;false&quot;/&gt;--&gt;</div><div class="line">        &lt;!--&lt;/property&gt;--&gt;</div><div class="line">        &lt;property name=&quot;picture&quot;&gt;</div><div class="line">            &lt;column name=&quot;picture&quot; sql-type=&quot;longblob&quot; not-null=&quot;false&quot;/&gt;</div><div class="line">        &lt;/property&gt;</div><div class="line">        &lt;!--组件属性--&gt;</div><div class="line">        &lt;component name=&quot;addressEntity&quot; class=&quot;Entity.Address&quot;&gt;</div><div class="line">            &lt;property name=&quot;postCode&quot;&gt;</div><div class="line">                &lt;column name=&quot;postCode&quot;/&gt;</div><div class="line">            &lt;/property&gt;</div><div class="line">            &lt;property name=&quot;phone&quot;&gt;</div><div class="line">                &lt;column name=&quot;phone&quot;/&gt;</div><div class="line">            &lt;/property&gt;</div><div class="line">            &lt;property name=&quot;address&quot;&gt;</div><div class="line">                &lt;column name=&quot;address&quot;/&gt;</div><div class="line">            &lt;/property&gt;</div><div class="line">        &lt;/component&gt;</div><div class="line">    &lt;/class&gt;</div><div class="line">&lt;/hibernate-mapping&gt;</div></pre></td></tr></table></figure></p>
<p>组件属性的所有property会在实体(Student)对应的表中生成相应的字段</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-1167c0a2c8bc27c5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="table"></p>
<h4 id="get和load"><a href="#get和load" class="headerlink" title="get和load"></a>get和load</h4><p><img src="http://upload-images.jianshu.io/upload_images/1846623-94bac01e3d4de9bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-0ea3a2cb2a74623d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="2"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/1846623-ced268c5f21986e5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="项目组成介绍"></p>

    </div>
    <p class="post-meta">
        <span class="post-cat">分类：
            <a class="cat-link" href="/coder957/categories/Java/">Java</a>|<a class="cat-link" href="/coder957/categories/Java/学习/">学习</a>
        </span>
        <span class="post-tags">
            标签：
            
    
        <a href="/coder957/tags/Java/" title="Java">Java</a> / 
    
        <a href="/coder957/tags/Maven/" title="Maven">Maven</a> / 
    
        <a href="/coder957/tags/Hibernate/" title="Hibernate">Hibernate</a>
    

        </span>
    </p>
</article>
<!-- 分享按钮 -->

  <div class="article-share clearfix text-center">
    <div class="share-area">
      <span class="share-txt">分享到：</span>
      <a href="javascript: window.open('http://service.weibo.com/share/share.php?url=' + encodeURIComponent(location.href) + '&title=' + document.title + '&language=zh_cn');" class="share-icon weibo"></a>
      <a href="javascript: alert('请复制链接到微信并发送');" class="share-icon wechat"></a>
      <a href="javascript: window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + encodeURIComponent(location.href) + '&title=' + document.title);" class="share-icon qqzone"></a>
      <a href="javascript: window.open('http://connect.qq.com/widget/shareqq/index.html?url=' + encodeURIComponent(location.href) + '&desc=Jelon个人博客&title=' + document.title + '&callback=' + encodeURIComponent(location.href));" class="share-icon qq"></a>
      <a href="javascript: window.open('http://shuo.douban.com/!service/share?href=' + encodeURIComponent(location.href) + '&name=' + document.title + '&text=' + document.title);" class="share-icon douban"></a>
    </div>
  </div>


    <!-- 上一篇/下一篇 -->

<div class="article-nav clearfix">
    
    <span class="prev fl">
        上一篇<br >
        <a href="/coder957/2017/09/28/hibernate-annotation/">
            
                hibernate-annotation
            
        </a>
    </span>
    

    
    <span class="next fr">
        下一篇<br >
        <a href="/coder957/2017/09/20/java-json/">
            
                Java处理Json
            
        </a>
    </span>
    
</div>

        <!-- 文章评论 -->
        
  <script src="/coder957/js/comment.js"></script>
  <div id="comments" class="comment">
    <!--
    <div class="sign-bar">
      GitHub 已登录!
      <span class="sign-link">登出</span>
    </div>
    <section class="box">
      <div class="com-avatar"><img src="/img/jelon.jpg" alt="avatar"></div>
      <div class="com-text">
        <div class="main">
          <textarea class="text-area-edited show" placeholder="欢迎评论！"></textarea>
          <div class="text-area-preview"></div>
        </div>
        <div class="switch">
          <div class="switch-item on">编辑</div>
          <div class="switch-item">预览</div>
        </div>
        <div class="button">提交</div>
      </div>
    </section>
    <section class="tips">注：评论支持 markdown 语法！</section>
    <section class="list-wrap">
      <ul class="list">
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/jelon.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">张德龙</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like liked">已赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">333333</div>
          </div>
        </li>
        <li>
          <div class="user-avatar">
            <a href="/">
              <img src="/img/jelon.jpg" alt="user-avatar">
            </a>
          </div>
          <div class="user-comment">
            <div class="user-comment-header">
              <span class="post-name">刘德华</span>
              <span class="post-time">2017年12月12日</span>
              <span class="like">点赞</span>
              <span class="like-num">2</span>
            </div>
            <div class="user-comment-body">vvvvv</div>
          </div>
        </li>
      </ul>
      <div class="page-nav">
        <a href="javascript: void(0);" class="item">1</a>
        <a href="javascript: void(0);" class="item">2</a>
        <a href="javascript: void(0);" class="item current">3</a>
      </div>
    </section>
    -->
  </div>
  <script>
  JELON.Comment({
    container: 'comments',
    label: 'Hibernate-Entity-hbm-xml实现-intellij-kotlin入门级' || '2017/09/26/Hibernate-Entity-hbm-xml实现-intellij-kotlin入门级/',
    owner: 'FutaoSmile',
    repo: 'blog_comments',
    clientId: '149ec394071cfd7f0f47',
    clientSecret: 'd1c988f53efa3f0f8651799f8212cc8e2cab24b9'
  });
  </script>


            </div>

        </section>
        <!-- 侧栏部分 -->
<aside class="sidebar">
    <section class="widget">
        <h3 class="widget-hd"><strong>文章分类</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
    <li>
        <a href="/coder957/categories/后端/">后端</a>
        <span class="badge">(5)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Java/">Java</a>
        <span class="badge">(8)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Web技术/">Web技术</a>
        <span class="badge">(3)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/后端/Java/">Java</a>
        <span class="badge">(5)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/GIt/">GIt</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Java/Spring/">Spring</a>
        <span class="badge">(3)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Java/学习/">学习</a>
        <span class="badge">(5)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Web技术/Java/">Java</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/Web技术/Kotlin/">Kotlin</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/GIt/Web技术/">Web技术</a>
        <span class="badge">(1)</span>
    </li>
    
    <li>
        <a href="/coder957/categories/学习/">学习</a>
        <span class="badge">(1)</span>
    </li>
    
</ul>
    </section>

    
    <section class="widget">
        <h3 class="widget-hd"><strong>热门标签</strong></h3>
        <!-- 文章标签 -->
<div class="widget-bd tag-wrap">
  
    <a class="tag-item" href="/coder957/tags/Java/" title="Java">Java (12)</a>
  
    <a class="tag-item" href="/coder957/tags/Spring/" title="Spring">Spring (9)</a>
  
    <a class="tag-item" href="/coder957/tags/kotlin/" title="kotlin">kotlin (7)</a>
  
    <a class="tag-item" href="/coder957/tags/Maven/" title="Maven">Maven (2)</a>
  
    <a class="tag-item" href="/coder957/tags/Hibernate/" title="Hibernate">Hibernate (2)</a>
  
    <a class="tag-item" href="/coder957/tags/kotlin-小知识/" title="kotlin 小知识">kotlin 小知识 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/Kotlin/" title="Kotlin">Kotlin (1)</a>
  
    <a class="tag-item" href="/coder957/tags/Json/" title="Json">Json (1)</a>
  
    <a class="tag-item" href="/coder957/tags/热点文章/" title="热点文章">热点文章 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/博主推荐/" title="博主推荐">博主推荐 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/转载/" title="转载">转载 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/资料/" title="资料">资料 (1)</a>
  
    <a class="tag-item" href="/coder957/tags/Ajax/" title="Ajax">Ajax (1)</a>
  
</div>
    </section>
    

    

    
    <!-- 友情链接 -->
    <section class="widget">
        <h3 class="widget-hd"><strong>友情链接</strong></h3>
        <!-- 文章分类 -->
<ul class="widget-bd">
    
        <li>
            <a href="http://lsc95.github.io/" target="_blank" title="椿哥的狗窝">椿哥的狗窝</a>
        </li>
    
        <li>
            <a href="http://futaosmile.oschina.io/documents/" target="_blank" title="donot click me">Document</a>
        </li>
    
        <li>
            <a href="" target="_blank" title="虚位以待">loading...</a>
        </li>
    
</ul>
    </section>
    
</aside>
<!-- / 侧栏部分 -->
    </div>

    <!-- 博客底部 -->
    <footer class="footer">
    &copy;
    
                2016-
                2017
                    

                        <a href="/coder957/">Loves You</a>
</footer>
<div class="back-to-top" id="JELON__backToTop" title="返回顶部">返回顶部</div>
    <!--博客js脚本 -->
    <!-- 这里放网站js脚本 -->
<script src="/coder957/js/main.js"></script>
</body>
</html>